<template>
  <div class="container">
    <div class="title-box flex justify-between">
      <div class="title">{{ translateTitle('Dams.HSM.列名') }}</div>
      <div class="handle">{{ translateTitle('Dams.HSM.操作') }}</div>
    </div>
    <div class="tree-box">
      <el-tree
        ref="tree"
        :allow-drag="() => true"
        :allow-drop="() => true"
        :data="listData"
        default-expand-all
        draggable
        node-key="id"
        :props="treeProps"
        show-checkbox
        @check-change="handleCheckChange"
      >
        <template #default="{ node, data }">
          <span class="custom-tree-node">
            <span>{{ node.label }}</span>
            <span>
              <a @click="edit(node, data)">{{ translateTitle('Dams.HSM.编辑') }}</a>
            </span>
          </span>
        </template>
      </el-tree>
    </div>
    <el-dialog v-model="dialogVisible" center destroy-on-close :title="translateTitle('Dams.HSM.请输入新的列名')" width="30%">
      <el-input v-model="columnTitle" :placeholder="columnPlaceholder" />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">{{ translateTitle('Dams.HSM.取消') }}</el-button>
          <el-button type="primary" @click="confirm">{{ translateTitle('Dams.HSM.确定') }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { translateTitle } from '@/utils/i18n'

  export default {
    name: 'DragTree',
    props: {
      list: {
        type: Array,
        default: () => [],
      },
    },
    data() {
      return {
        treeProps: {
          label: 'title',
          children: 'children',
        },
        listData: this.list,
        dialogVisible: false,
        columnTitle: '',
        columnPlaceholder: '',
        selectedNode: {},
        selectedData: {},
      }
    },
    computed: {},
    watch: {
      dialogVisible() {
        this.columnTitle = ''
      },
    },
    mounted() {
      //设置默认全选
      this.$refs.tree.setCheckedNodes(this.listData)
    },
    methods: {
      translateTitle,
      confirm() {
        try {
          const parent = this.selectedNode.parent
          const children = parent.data.children || parent.data
          const index = children.findIndex((d) => d.id === this.selectedData.id)
          children[index].title = this.columnTitle
          this.dialogVisible = false
          this.$message({
            type: 'success',
            message: translateTitle('Dams.HSM.编辑成功'),
          })
        } catch (error) {
          this.$message({
            type: 'error',
            message: translateTitle('Dams.HSM.编辑失败'),
          })
        }
      },
      edit(node, data) {
        this.selectedNode = node
        this.selectedData = data
        this.dialogVisible = true
        this.columnPlaceholder = data.title
      },
      //节点选中状态发生变化时修改参数
      handleCheckChange(node, check) {
        node.checked = check
      },
    },
  }
</script>

<style lang="scss" scoped>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 4.7vw;
  }
  .tree-box {
    // width: 100%;
    margin: 0 auto;
    overflow-y: auto;
    max-height: 546px;
  }
  .title-box {
    margin-top: 2px;
    div {
      background-color: #88c6e4;
      color: #000000;
      font-weight: bold;
      width: 100%;
      line-height: 2;
      text-align: center;
    }
    .handle {
      margin-left: 2px;
      width: 30%;
    }
  }
</style>
